using System;
using System.Web;
using Edge.Common.DataUtils;
using Edge.Common.Utils;
using Edge.Common.Utils;

namespace Edge.Common.Web.DataUtils
{
    public class SessionVariable<T>
    //        where T : class
    {
        private readonly string key;
        private readonly T defaultValue;

        public SessionVariable(string key)
            : this(key, default(T))
        {
        }


        public SessionVariable(string key, T defaultValue)
        {
            if (StringUtils.IsBlankOrNull(key))
                throw new ArgumentException("key");
            this.key = key;
            this.defaultValue = defaultValue;
        }

        public T Value
        {
            get
            {
                HttpContext context = HttpContext.Current;

                if (context == null)
                    throw new InvalidOperationException("http context does not exists");

                if (context.Session.Contents[key] == null)
                    context.Session.Contents[key] = defaultValue;

                return (T)context.Session.Contents[key];
            }
            set
            {
                HttpContext context = HttpContext.Current;

                if (context == null)
                    throw new InvalidOperationException("http context does not exists");

                context.Session.Contents[key] = value;
            }
        }
    }
}